package com.sunronsoft.mind.map;

import com.jfinal.core.Controller;
import com.jfinal.ext.kit.DateKit;
import com.jfinal.kit.*;
import com.jfinal.upload.UploadFile;
import com.sunronsoft.mind.map.model.MindMap;

import java.io.File;
import java.util.Date;

/**
 * @author Sunron
 **/
public class MindmapController extends Controller {
    
    private static MindService mindService = MindService.getInstance();
    private static UserService userService = UserService.getInstance();
    
    public void chart() {
        String token = getHeader("token");
        Kv mapList = mindService.chartList();
        renderJson(DataKit.toSuccess(mapList));
    }
    
    public void list() {
        renderJson(DataKit.toSuccess(mindService.documentList()));
    }
    
    public void category() {
        renderJson(DataKit.toSuccess(mindService.categoryList()));
    }
    
    public void parentRoot() {
        renderJson(DataKit.toSuccess(mindService.parentList()));
    }
    
    public void detail() {
        Kv params = JsonKit.parse(getRawData(), Kv.class);
        if (params == null || params.isEmpty()) {
            renderJson(DataKit.toFail("参数错误"));
        } else if (!params.containsKey("mindId")) {
            renderJson(DataKit.toFail("参数错误"));
        } else {
            renderJson(DataKit.toSuccess(mindService.detail(Integer.parseInt(params.getStr("mindId")))));
        }
    }
    
    public void upload() {
        try {
            UploadFile upfile = getFile();
            if (null == upfile) {
                renderJson(DataKit.toSuccess(""));
                return;
            }
            String filename = upfile.getOriginalFileName();
            if (StrKit.notBlank(filename)) {
                String path = PathKit.getWebRootPath() + "/" + MainConfig.prop.get("uploadPath");
                DataKit.processDirectory(path);
                
                filename = DateKit.toStr(new Date(), "yyyyMMddHHmmss") + "_" + filename;
                final File saveFile = new File(path + "/" + filename);
                boolean saveResult = upfile.getFile().renameTo(saveFile);
                if (saveResult) {
                    renderJson(DataKit.toSuccess(filename));
                } else {
                    renderJson(DataKit.toFail("文件上传错误"));
                }
            } else {
                renderJson(DataKit.toFail("文件上传错误"));
            }
        } catch (Exception e) {
            LogKit.error(e.getMessage(), e);
            renderJson(DataKit.toFail("文件上传错误"));
        }
    }
    
    public void save() {
        Kv params = JsonKit.parse(getRawData(), Kv.class);
        if (params == null || params.isEmpty()) {
            renderJson(DataKit.toFail("参数错误"));
        } else if (StrKit.isBlank(params.getStr("topic")) || StrKit.isBlank(params.getStr("parentId"))) {
            renderJson(DataKit.toFail("参数错误"));
        } else {
            try {
                MindMap map = new MindMap();
                map.setId(StrKit.isBlank(params.getStr("id")) ? 0 : Integer.parseInt(params.getStr("id")));
                map.setTopic(params.getStr("topic"));
                map.setType(Integer.parseInt(params.getStr("type")));
                map.setParentId(Integer.parseInt(params.getStr("parentId")));
                map.setPath(params.getStr("path"));
                
                if (Integer.parseInt(params.getStr("type")) == MindMap.TYPE) {
                    map.setExpanded(!StrKit.isBlank(params.getStr("expanded")) && Boolean.parseBoolean(params.getStr("expanded")));
                } else {
                    map.setExpanded(StrKit.isBlank(params.getStr("expanded")) || Boolean.parseBoolean(params.getStr("expanded")));
                }
                
                map.setDirection(StrKit.isBlank(params.getStr("direction")) ? MindMap.RIGHT : params.getStr("direction"));
                
                if (mindService.save(map)) {
                    renderJson(DataKit.toSuccess("数据保存成功"));
                } else {
                    renderJson(DataKit.toFail("数据保存失败"));
                }
            } catch (Exception e) {
                LogKit.error(e.getMessage(), e);
                renderJson(DataKit.toFail("数据保存失败"));
            }
        }
    }
    
    public void delete() {
        Kv params = JsonKit.parse(getRawData(), Kv.class);
        if (params == null || params.isEmpty()) {
            renderJson(DataKit.toFail("参数错误"));
        } else if (!params.containsKey("mindId")) {
            renderJson(DataKit.toFail("参数错误"));
        } else if (mindService.delete(Integer.parseInt(params.getStr("mindId")))) {
            renderJson(DataKit.toSuccess("0".equals(params.getStr("mindType")) ? "删除分类成功" : "删除文档成功"));
        } else {
            renderJson(DataKit.toFail("0".equals(params.getStr("mindType")) ? "删除分类失败" : "删除文档失败"));
        }
    }
}
